/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fuzzy.actions;

import fuzzy.hibernate.Funkcje;
import fuzzy.hibernate.HibernateUtil;
import fuzzy.hibernate.Klienci;
import fuzzy.hibernate.Uzytkownicy;
import java.io.PrintWriter;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author michal
 */
public class AjaxAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    public ActionForward deleteClient(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        // Pobieramy ID klienta.
        String clientId = request.getParameter("clientId");
        Session currentSession = HibernateUtil.getSession();
        currentSession.beginTransaction();
        
        // Pobieramy dane klienta z bazy.
        Query q = currentSession.createQuery("FROM Klienci WHERE idklienta = :clientId");
        q.setParameter("clientId", clientId);
        Klienci client = (Klienci) q.uniqueResult();
        // Usuwamy wybranego klienta.
        currentSession.delete(client.getUzytkownicy());
        currentSession.delete(client);
        currentSession.getTransaction().commit();
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print("OK");
        out.flush();
        
        return null;
    }

    public ActionForward checkUsername(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        // Pobieramy nazwę użytkownika.
        String username = request.getParameter("username");
        Session currentSession = HibernateUtil.getSession();
        currentSession.beginTransaction();
        
        // Pobieramy użytkowników o podanej nazwie
        Query q = currentSession.createQuery("FROM Uzytkownicy WHERE login = :username");
        q.setParameter("username", username);
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        // Sprawdzamy, czy istnieją użytkownicy o podanej nazwie.
        if (q.list().isEmpty()) {
            out.print("OK");
        } else {
            out.print("ERROR");
        }
        
        out.flush();
        
        return null;
    }
    
    public ActionForward manageClient(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        int clientId = Integer.parseInt(request.getParameter("clientId"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String pesel = request.getParameter("pesel");
        String address = request.getParameter("address");
        
        Klienci client;
        Uzytkownicy user;
        
        Session currentSession = HibernateUtil.getSession();
        currentSession.beginTransaction();
        
        // Sprawdzamy, czy użytkownik chce edytować dane klienta.
        if (clientId > 0) {
            Query q = currentSession.createQuery("FROM Klienci WHERE idklienta = :clientId");
            q.setParameter("clientId", clientId);
            client = (Klienci) q.uniqueResult();
            user = client.getUzytkownicy();
        }
        // Rejestracja nowego klienta.
        else {
            client = new Klienci(); // Encja klienta.
            user = new Uzytkownicy(); // Encja użytkownika.
        }
        
        // Zapisujemy dane do obiektów.
        client.setImie(firstName);
        client.setNazwisko(lastName);
        client.setPesel(pesel);
        client.setAdres(address);
        user.setLogin(username);
        if (password != null && password.length() > 0) {
            user.setHaslo(password);
        }
        client.setUzytkownicy(user);
        
        currentSession.saveOrUpdate(user);
        currentSession.saveOrUpdate(client);
        currentSession.getTransaction().commit();
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println(clientId);
        out.print(username);
        out.flush();
        
        return null;
    }
    
    public ActionForward manageFunction(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        int functionId = Integer.parseInt(request.getParameter("functionId"));
        BigDecimal value = new BigDecimal(request.getParameter("value"));
        
        Funkcje function;
        
        Session currentSession = HibernateUtil.getSession();
        currentSession.beginTransaction();
        
        // Sprawdzamy, czy użytkownik chce edytować dane klienta.
        if (functionId > 0) {
            Query q = currentSession.createQuery("FROM Funkcje WHERE fun_nr = :functionId");
            q.setParameter("functionId", functionId);
            function = (Funkcje) q.uniqueResult();
        }
        // Rejestracja nowego klienta.
        else {
            function = new Funkcje(); // Encja klienta.
        }
        
        // Zapisujemy dane do obiektów.
        function.setWartosc(value);
        
        currentSession.saveOrUpdate(function);
        currentSession.getTransaction().commit();
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print("OK");
        out.flush();
        
        return null;
    }
}
